import router from './router'
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
    document.title = getPageTitle(to.meta.title)

    const token = getToken();
    if (token) {
        if (to.path === '/login') {
            next('/home')
        } else {
            next()
        }
    }
    else {
        if (whiteList.includes(to.path)) {
            next()
        } else {
            next(`/login`)
        }
    }
})
